import jieba

with open('无际涯.txt', 'r', encoding='utf-8') as file:
    s = file.read()
print(s)

# 分词
lst = jieba.lcut(s)
# print(lst)
set1 = set(lst) # 将列表转成集合的目的 是为了去重
d = {} # 使用字典存储词频 key: 词 value: 出现的次数
for item in set1:
    if len(item) >= 2:
        d[item] = 0

# 遍历列表统计词频
for item in lst:
    if item in d:
        d[item] = d[item] + 1

# 将字典转成列表类型 [词,次数] [好,10]
new_lst = []
for item in d:
    new_lst.append([item, d[item]])

# 列表排序
new_lst.sort(key=lambda x:x[1], reverse=True)
print(new_lst[0:11])

